package rest;

import org.restlet.data.MediaType;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import java.io.IOException;
import java.util.HashMap;

public class RestletRestClient {

    /**
     * 经测试，使用restlet方式调用rest服务，如果rest返回的内容不是json，会报错（不是json的返回内容处理方法，暂时未找到）
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
//        ClientResource client = new ClientResource("http://localhost:7778/query/1334");
//        Representation representation = client.get(MediaType.APPLICATION_JSON);
        //创建jacksonRepresentation实例，将响应内容转换为map
//        JacksonRepresentation jr = new JacksonRepresentation(representation, HashMap.class);
//        HashMap object = (HashMap) jr.getObject();
//        System.out.println("遍历输出map的结果");
        ClientResource client = new ClientResource("http://localhost:7778//getUser/jojo");
        Representation representation = client.get(MediaType.APPLICATION_JSON);
        JacksonRepresentation jr = new JacksonRepresentation(representation, HashMap.class);
        HashMap object = (HashMap) jr.getObject();
        System.out.println("user id :" + object.get("userId"));
        System.out.println("user name:" + object.get("userName"));
    }
}
